﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Threading;
using System.IO;

namespace Ajax.Web.Install
{
    public partial class Index : System.Web.UI.Page
    {
        protected string installInfo = string.Empty;
        private void page_load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string str = Server.MapPath("db/");
                string path = str + "Accessinstall.lock";
                if (File.Exists(path))
                {
                    this.installInfo = "</br></br>系统检测到本机已安装Access数据库(如需再次安装，请删除db目录下Accessinstall.lock文件)</br></br><a href='../Index.html' style='color:red'>点击此处跳转到网站首页</a>";
                    this.AccessBtn.Visible = false;
                    this.sqlserverBtn.Visible = false;
                }
            }
        }

        #region  选择Access数据库
        protected void AccessBtn_Click(object sender, ImageClickEventArgs e)
        {
            XmlDocument document = new XmlDocument();
            document.Load(base.Server.MapPath("~/Web.config"));
            XmlNode node = document.SelectSingleNode("//configuration/appSettings/add[@key='FactoryKey']");
            if (node != null)
            {
                string str = Server.MapPath("db/");
                string path = str + "Accessinstall.lock";
                node.Attributes[1].Value = "Ajax.AccessDAL";
                document.Save(Server.MapPath("~/Web.config"));
                Thread.Sleep(500);
                this.installInfo = "安装成功。系统正在为您跳转到网站首页";
                File.WriteAllText(path, "删除此文件就能安装啦");
                Thread.Sleep(500);
                Response.Redirect("~/Index.html"); 
            }
            else
            {
                this.installInfo = "请检查配置文件是否存在FactoryKey节点";
            }
        }
        #endregion

        #region 选择MSSQL数据库
        protected void sqlserverBtn_Click(object sender, ImageClickEventArgs e)
        {
            XmlDocument document = new XmlDocument();
            document.Load(base.Server.MapPath("~/Web.config"));
            XmlNode node = document.SelectSingleNode("//configuration/appSettings/add[@key='FactoryKey']");
            if (node != null)
            {
                node.Attributes[1].Value = "Ajax.SQLserverDAL";
                document.Save(Server.MapPath("~/Web.config"));
                Thread.Sleep(500);
                Response.Redirect("Step.html");
            }
            else
            {
                this.installInfo = "请检查配置文件是否存在FactoryKey节点";
            }
        }
        #endregion
    }
}
